<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="仓库">
              <j-dict-select-tag
                default-select-first
                placeholder="请选择仓库"
                v-model="queryParam.warehouseCode"
                dictCode="wms_warehouse,warehouse_name,warehouse_code" />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="物料名称">
              <a-input placeholder="请输入物料名称" v-model="queryParam.materialName"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="物料编码">
              <a-input placeholder="请输入物料编码" v-model="queryParam.materialCode"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAddTo" type="primary" icon="plus">添加</a-button>
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('出库单表')">导出</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a
        style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="outboundOrderItemId"
        class="j-table-force-nowrap1"
        :scroll="{x:true}"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange">
      </a-table>
    </div>

    <outbound-record-new-modal ref="newModalForm" @ok="modalFormOk" />
    <outbound-record-modal ref="modalForm" @ok="modalFormOk" />
    <outbound-record-with-item-modal ref="withItemModalForm" @ok="modalFormOk" />
  </a-card>
</template>

<script>

import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import OutboundRecordModal from './modules/OutboundRecordModal.vue'
import OutboundRecordNewModal from './modules/OutboundRecordNewModal.vue'
import { filterMultiDictText } from '@/components/dict/JDictSelectUtil'
import '@/assets/less/TableExpand.less'
import OutboundRecordWithItemModal from './modules/OutboundRecordWithItemModal.vue'

export default {
  name: 'OutboundOrderList',
  mixins: [JeecgListMixin],
  components: {
    OutboundRecordWithItemModal,
    OutboundRecordNewModal,
    OutboundRecordModal
  },
  data() {
    return {
      description: '出库单表管理页面',
      disableMixinCreated: true,
      // 表头
      columns1: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 40,
          align: 'center',
          fixed: 'left',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '出库类型',
          align: 'center',
          dataIndex: 'outboundType_dictText',
          width: 80,
          fixed: 'left'
        },
        {
          title: '单据号',
          align: 'center',
          dataIndex: 'documentNumber',
          width: 80,
          fixed: 'left'
        },
        /*
        {
          title: '出库单号',
          align: 'center',
          dataIndex: 'outboundNumber',
          width: 80,
          fixed: 'left'
        },
         */
        {
          title: '已出库数量',
          align: 'center',
          dataIndex: 'outboundQuantity'
        },
        {
          title: '未出库数量',
          align: 'center',
          dataIndex: 'unOutboundQuantity'
        },
        {
          title: '客户物料编码',
          align: 'center',
          dataIndex: 'customerMaterialCode'
        },
        {
          title: '物料编码',
          align: 'center',
          dataIndex: 'materialCode'
        },
        {
          title: '物料名称',
          align: 'center',
          dataIndex: 'materialName',
          width: 120
        },
        {
          title: '规格型号',
          align: 'center',
          dataIndex: 'specificationModel'
        },
        {
          title: '单位',
          align: 'center',
          dataIndex: 'unit_dictText'
        },
        {
          title: '数量',
          align: 'center',
          dataIndex: 'quantity'
        },
        {
          title: '辅单位',
          align: 'center',
          dataIndex: 'secondaryUnit_dictText'
        },
        {
          title: '辅数量',
          align: 'center',
          dataIndex: 'secondaryQuantity'
        },
        {
          title: '库存可用数量',
          align: 'center',
          dataIndex: 'availableInventory'
        },
        {
          title: '收货地址',
          align: 'center',
          dataIndex: 'deliveryAddress'
        },
        {
          title: '领料仓库',
          align: 'center',
          dataIndex: 'warehouseCode_dictText'
        },
        {
          title: '发货/领料日期',
          align: 'center',
          dataIndex: 'issueDate',
          customRender: function(text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '制单人',
          align: 'center',
          dataIndex: 'createBy_dictText'
        },
        {
          title: '制单部门',
          align: 'center',
          dataIndex: 'sysOrgCode_dictText'
        },
        {
          title: '制单日期',
          align: 'center',
          dataIndex: 'createTime',
          customRender: function(text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '备注',
          align: 'center',
          dataIndex: 'remark'
        },
      ],
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 40,
          align: 'center',
          fixed: 'left',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '出库类型',
          align: 'center',
          dataIndex: 'outboundType_dictText',
          width: 80,
          fixed: 'left',
          sorter: (a, b) => {
            const valA = a.outboundType_dictText || '';
            const valB = b.outboundType_dictText || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '单据号',
          align: 'center',
          dataIndex: 'documentNumber',
          width: 80,
          fixed: 'left',
          sorter: (a, b) => {
            const valA = a.documentNumber || '';
            const valB = b.documentNumber || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '已出库数量',
          align: 'center',
          dataIndex: 'outboundQuantity',
          sorter: (a, b) => {
            const valA = a.outboundQuantity;
            const valB = b.outboundQuantity;
            if (valA == null && valB == null) return 0;
            if (valA == null) return 1;
            if (valB == null) return -1;
            return Number(valA) - Number(valB);
          },
        },
        {
          title: '未出库数量',
          align: 'center',
          dataIndex: 'unOutboundQuantity',
          sorter: (a, b) => {
            const valA = a.unOutboundQuantity;
            const valB = b.unOutboundQuantity;
            if (valA == null && valB == null) return 0;
            if (valA == null) return 1;
            if (valB == null) return -1;
            return Number(valA) - Number(valB);
          },
        },
        {
          title: '客户物料编码',
          align: 'center',
          dataIndex: 'customerMaterialCode',
          sorter: (a, b) => {
            const valA = a.customerMaterialCode || '';
            const valB = b.customerMaterialCode || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '物料编码',
          align: 'center',
          dataIndex: 'materialCode',
          sorter: (a, b) => {
            const valA = a.materialCode || '';
            const valB = b.materialCode || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '物料名称',
          align: 'center',
          dataIndex: 'materialName',
          width: 120,
          sorter: (a, b) => {
            const valA = a.materialName || '';
            const valB = b.materialName || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '规格型号',
          align: 'center',
          dataIndex: 'specificationModel',
          sorter: (a, b) => {
            const valA = a.specificationModel || '';
            const valB = b.specificationModel || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '单位',
          align: 'center',
          dataIndex: 'unit_dictText',
          sorter: (a, b) => {
            const valA = a.unit_dictText || '';
            const valB = b.unit_dictText || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '数量',
          align: 'center',
          dataIndex: 'quantity',
          sorter: (a, b) => {
            const valA = a.quantity;
            const valB = b.quantity;
            if (valA == null && valB == null) return 0;
            if (valA == null) return 1;
            if (valB == null) return -1;
            return Number(valA) - Number(valB);
          },
        },
        {
          title: '辅单位',
          align: 'center',
          dataIndex: 'secondaryUnit_dictText',
          sorter: (a, b) => {
            const valA = a.secondaryUnit_dictText || '';
            const valB = b.secondaryUnit_dictText || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '辅数量',
          align: 'center',
          dataIndex: 'secondaryQuantity',
          sorter: (a, b) => {
            const valA = a.secondaryQuantity;
            const valB = b.secondaryQuantity;
            if (valA == null && valB == null) return 0;
            if (valA == null) return 1;
            if (valB == null) return -1;
            return Number(valA) - Number(valB);
          },
        },
        {
          title: '库存可用数量',
          align: 'center',
          dataIndex: 'availableInventory',
          sorter: (a, b) => {
            const valA = a.availableInventory;
            const valB = b.availableInventory;
            if (valA == null && valB == null) return 0;
            if (valA == null) return 1;
            if (valB == null) return -1;
            return Number(valA) - Number(valB);
          },
        },
        {
          title: '收货地址',
          align: 'center',
          dataIndex: 'deliveryAddress',
          sorter: (a, b) => {
            const valA = a.deliveryAddress || '';
            const valB = b.deliveryAddress || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '领料仓库',
          align: 'center',
          dataIndex: 'warehouseCode_dictText',
          sorter: (a, b) => {
            const valA = a.warehouseCode_dictText || '';
            const valB = b.warehouseCode_dictText || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '发货/领料日期',
          align: 'center',
          dataIndex: 'issueDate',
          customRender: function(text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          },
          sorter: (a, b) => {
            const valA = a.issueDate || '';
            const valB = b.issueDate || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '制单人',
          align: 'center',
          dataIndex: 'createBy_dictText',
          sorter: (a, b) => {
            const valA = a.createBy_dictText || '';
            const valB = b.createBy_dictText || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '制单部门',
          align: 'center',
          dataIndex: 'sysOrgCode_dictText',
          sorter: (a, b) => {
            const valA = a.sysOrgCode_dictText || '';
            const valB = b.sysOrgCode_dictText || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '制单日期',
          align: 'center',
          dataIndex: 'createTime',
          customRender: function(text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          },
          sorter: (a, b) => {
            const valA = a.createTime || '';
            const valB = b.createTime || '';
            return valA.localeCompare(valB, 'en');
          },
        },
        {
          title: '备注',
          align: 'center',
          dataIndex: 'remark'
        },
      ],
      url: {
        list: '/wms/outbound/outbound-order/list',
        delete: '/wms/outbound/outbound-order/delete',
        deleteBatch: '/wms/outbound/outbound-order/deleteBatch',
        exportXlsUrl: '/wms/outbound/outbound-order/exportXls',
        importExcelUrl: 'outbound/outboundOrder/importExcel'

      },
      dictOptions: {},
      superFieldList: [],
      queryParam: {
        warehouseCode: ''
      }
    }
  },
  watch: {
    'queryParam.warehouseCode': {
      handler(newVal) {
        if (newVal) {
          this.loadData()
        }
      },
      immediate: true
    }
  },
  created() {
    if (this.queryParam.warehouseCode) {
      this.loadData()
    }
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  methods: {
    initDictConfig() {
    },
    handleAdd() {
      this.$refs.newModalForm.add()
      this.$refs.newModalForm.title = '新增'
      this.$refs.newModalForm.warehouseCode = this.queryParam.warehouseCode
      this.$refs.newModalForm.disableSubmit = false
    },
    handleAddTo() {
      if (!this.selectionRows || this.selectionRows.length === 0) {
        this.$message.warning('请先选中一条记录')
        return
      }
      const { entryNumber, outboundType } = this.selectionRows[0]
      if (this.selectionRows.some(row => row.entryNumber !== entryNumber)) {
        this.$message.warning('请选择同一单据的记录')
        return
      }
      if ([4, 5].includes(outboundType)) {
        this.$refs.withItemModalForm.add(this.selectionRows)
        this.$refs.withItemModalForm.title = '添加'
        this.$refs.withItemModalForm.disableSubmit = false
        return
      }
      this.$refs.modalForm.add(this.selectionRows)
      this.$refs.modalForm.title = '添加'
      this.$refs.modalForm.warehouseCode = this.queryParam.warehouseCode
      this.$refs.modalForm.disableSubmit = false
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>